<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px">
      <!--      <el-form-item label="课程名称" prop="courseName">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.courseName"-->
      <!--          placeholder="请输入课程名称"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="随堂测试名称" prop="testName">

        <el-input
          v-model="queryParams.testName"
          placeholder="请输入随堂测试名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="测试当前状态" prop="testStatus">
        <el-select v-model="queryParams.testStatus" placeholder="请选择测试的当前状态" clearable>
          <el-option
            v-for="dict in dict.type.test_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['classTests:classTests:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['classTests:classTests:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['classTests:classTests:remove']"
        >删除
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['classTests:classTests:export']"-->
      <!--        >导出</el-button>-->
      <!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="classTestsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="随堂测试id" align="center" prop="testId" />-->
      <el-table-column label="课程名称" align="center" prop="courseName"/>
      <el-table-column label="随堂测试名称" align="center" prop="testName"/>
      <!--      <el-table-column label="随堂测试日期" align="center" prop="testDate">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.testDate, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="开始时间" align="center" prop="startTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="时间(分钟)" align="center" prop="duration"/>
<!--      <el-table-column label="测试地点" align="center" prop="testLocation"/>-->
      <el-table-column label="测试形式" align="center" prop="testFormat"/>
      <el-table-column label="总分值" align="center" prop="totalMarks"/>
      <el-table-column label="及格分数线" align="center" prop="passingMarks"/>
      <!--      <el-table-column label="对测试规则和要求的说明" align="center" prop="testInstructions" />-->
      <!--      <el-table-column label="教师ID" align="center" prop="creatorTeacherId" />-->
      <el-table-column label="测试当前状态" align="center" prop="testStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.test_status" :value="scope.row.testStatus"/>
        </template>
      </el-table-column>
      <!--      <el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="skipClassTestsDetails(scope.row)"
          >添加测试题
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="startTest(scope.row)"
          >发起随堂测试
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="skipTestRecords(scope.row)"
          >查看学生测试
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['classTests:classTests:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['classTests:classTests:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改随堂测试管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <!--        <el-form-item label="课程ID" prop="courseId">-->
        <!--          <el-input v-model="form.courseId" placeholder="请输入课程ID" />-->
        <!--        </el-form-item>-->
        <el-form-item label="随堂测试名称" prop="testName">
          <el-input v-model="form.testName" placeholder="请输入随堂测试名称"/>
        </el-form-item>
        <!--        <el-form-item label="随堂测试日期" prop="testDate">-->
        <!--          <el-date-picker clearable-->
        <!--            v-model="form.testDate"-->
        <!--            type="date"-->
        <!--            value-format="yyyy-MM-dd"-->
        <!--            placeholder="请选择随堂测试日期">-->
        <!--          </el-date-picker>-->
        <!--        </el-form-item>-->
        <el-form-item label="开始具体时间" prop="startTime">
          <el-date-picker clearable
                          v-model="form.startTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择随堂测试开始的具体时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束具体时间" prop="endTime">
          <el-date-picker clearable
                          v-model="form.endTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择随堂测试结束的具体时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="时间(分钟)" prop="duration">
          <el-input-number v-model="form.duration" placeholder="请输入时间(分钟)"/>
        </el-form-item>
<!--        <el-form-item label="测试地点" prop="testLocation">-->
<!--          <el-input v-model="form.testLocation" placeholder="请输入测试的地点或线上"/>-->
<!--        </el-form-item>-->
        <el-form-item label="测试形式" prop="testFormat">
          <el-input v-model="form.testFormat" placeholder="请输入测试的形式(例：选择题，判断题)"/>
        </el-form-item>
        <el-form-item label="总分值" prop="totalMarks">
          <el-input v-model="form.totalMarks" placeholder="请输入测试的总分值"/>
        </el-form-item>
        <el-form-item label="及格分数线" prop="passingMarks">
          <el-input v-model="form.passingMarks" placeholder="请输入测试的及格分数线"/>
        </el-form-item>
        <el-form-item label="规则和要求说明" prop="testInstructions">
          <el-input v-model="form.testInstructions" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <!--        <el-form-item label="教师ID" prop="creatorTeacherId">-->
        <!--          <el-input v-model="form.creatorTeacherId" placeholder="请输入教师ID" />-->
        <!--        </el-form-item>-->
        <el-form-item label="当前状态" prop="testStatus">
          <el-select v-model="form.testStatus" placeholder="请选择测试的当前状态">
            <el-option
              v-for="dict in dict.type.test_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  startTest,
  listClassTests,
  getClassTests,
  delClassTests,
  addClassTests,
  updateClassTests
} from "@/api/classTests/classTests";
import server from "@/views/monitor/server/index.vue";

export default {
  name: "ClassTests",
  dicts: ['test_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 随堂测试管理表格数据
      classTestsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // open2: false,
      // 备注时间范围
      daterangeTestDate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        courseId: null,
        courseName: null,
        testName: null,
        creatorTeacherId: null,
        testStatus: null,
      },
      params: {
        courseId: null,
        testId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        testName: [
          {required: true, message: "随堂测试名称不能为空", trigger: "blur"}
        ],
        startTime: [
          {required: true, message: "随堂测试开始的具体时间不能为空", trigger: "blur"}
        ],
        endTime: [
          {required: true, message: "随堂测试结束的具体时间不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.queryParams.courseId = this.$route.query.courseId;
    this.getList();
  },
  methods: {
    skipTestRecords(row) {
      this.$router.push({ path: "/teacher/testRecords", query: {testId: row.testId} });
    },
    startTest(row){
      this.params.courseId = row.courseId
      this.params.testId = row.testId
      this.loading = true
      startTest(this.params).then(response => {
        this.loading = false
        if (response.data == 1){
          this.$modal.msgSuccess("发起成功");
        }else {
          this.$modal.msgWarning("请先添加测试题后再发起");
        }

      });
    },
    skipClassTestsDetails(row) {
      this.$router.push({ path: "/teacher/classTestsDetails", query: {testId: row.testId} });
    },
    /** 查询随堂测试管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeTestDate && '' != this.daterangeTestDate) {
        this.queryParams.params["beginTestDate"] = this.daterangeTestDate[0];
        this.queryParams.params["endTestDate"] = this.daterangeTestDate[1];
      }
      listClassTests(this.queryParams).then(response => {
        this.classTestsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        testId: null,
        courseId: null,
        testName: null,
        testDate: null,
        startTime: null,
        endTime: null,
        duration: null,
        testLocation: null,
        testFormat: null,
        totalMarks: null,
        passingMarks: null,
        testInstructions: null,
        creatorTeacherId: null,
        testStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeTestDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.testId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加随堂测试管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const testId = row.testId || this.ids
      getClassTests(testId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改随堂测试管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.courseId = this.queryParams.courseId
          if (this.form.testId != null) {
            updateClassTests(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClassTests(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const testIds = row.testId || this.ids;
      this.$modal.confirm('是否确认删除随堂测试管理编号为"' + testIds + '"的数据项？').then(function () {
        return delClassTests(testIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('classTests/classTests/export', {
        ...this.queryParams
      }, `classTests_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
